import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/user/login'
import Index from '../views/index'
import User from '../views/user/User'
import Category from '../views/article/Category'
import AddArticle from '../views/article/AddArticle'
import Article from '../views/article/Article'
import Comment from '../views/comment/Comment'
import ArticleDeatil from '../views/article/ArticleDetail'
import UpdateArticle from '../views/article/UpdateArticle'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/index',
    component: Index,
    redirect: '/index/main',
    children: [
      {
        path: '/index/main',
        component: () => import('../views/Main.vue')
      },
      {
        path: '/index/user',
        component: User
      },
      {
        path: '/index/category',
        component: Category
      },
      {
        path: '/index/addArticle',
        component: AddArticle
      },
      {
        path: '/index/article',
        component: Article
      },
      {
        path: '/index/comment',
        component: Comment
      },
      {
        path: '/index/articleDeatil/:id',
        component: ArticleDeatil
      },
      {
        path: '/index/updateArticle/:id',
        component: UpdateArticle
      },
      {
        path: '/index/inform',
        component: () => import('../views/inform/Inform.vue')
      },
      {
        path: '/index/message',
        component: () => import('../views/message/Message.vue')
      },
      {
        path: '/index/book',
        component: () => import('../views/book/Book.vue')
      },
      {
        path: '/index/diary',
        component: () => import('../views/diary/Diary.vue')
      },
      {
        path: '/index/updateLog',
        component: () => import('../views/updateLog/UpdateLog.vue')
      },
      {
        path: '/index/friendBlog',
        component: () => import('../views/friendBlog/FriendBlog.vue')
      },
      {
        path: '/index/nav',
        component: () => import('../views/index/Nav.vue')
      },
      {
        path: '/index/tag',
        component: () => import('../views/index/Tag.vue')
      },
      {
        path: '/index/advice',
        component: () => import('../views/index/Advice.vue')
      },
      {
        path: '/index/bug',
        component: () => import('../views/index/Bug.vue')
      }
    ]
  }
  /* {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/!* webpackChunkName: "about" *!/ '../views/About.vue')
  } */
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从那个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行     next('/login') 强制跳转
  if (to.path === '/login') {
    return next()
  }
  // 检查是否登录
  const token = window.sessionStorage.getItem('lhamster_identity_info')
  // 检查是否为管理员
  const admin = window.sessionStorage.getItem('admin')
  if (!token || !admin) {
    window.sessionStorage.removeItem('lhamster_identity_info')
    window.sessionStorage.removeItem('headpicture')
    window.sessionStorage.removeItem('nickname')
    window.sessionStorage.removeItem('admin')
    alert('权限不足!');
    return next('/login')
  }
  next()
})
export default router
